package com.agilegroup.crm.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Window;
import android.view.WindowManager;

import com.actionbarsherlock.app.SherlockDialogFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.agilegroup.crm.R;
import com.agilegroup.crm.ui.fragments.ConnectionDialogFragment;
import com.agilegroup.crm.utils.OnlineChecker;
import com.agilegroup.crm.utils.SessionManager;

/**
 * @author Hisham
 *
 */
public class SplashActivity extends SherlockFragmentActivity {

	private SessionManager sessionManager;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sessionManager = new SessionManager(getApplicationContext());
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.splash);
        new CountDownTimer(3000,5000) {
            @Override
            public void onFinish() {
            	OnlineChecker checker = new OnlineChecker(SplashActivity.this);
            	if(checker.isOnline()==true){
            		 sessionManager.checkLogin();
            		 if(sessionManager.isLoggedIn()==true){
            			 SplashActivity.this.startActivity(new Intent(SplashActivity.this, MainActivity.class));
            		 }
            		 finish();
            	}
            	else{
            		//SplashActivity.this.startActivity(new Intent(SplashActivity.this, ConnectionDialogActivity.class));
            		SherlockDialogFragment dialogFragment = new ConnectionDialogFragment();
        			dialogFragment.show(getSupportFragmentManager(), "connectionDialog");
            	}
            }
            @Override
            public void onTick(long millisUntilFinished) {
            }
        }.start();
 
    }
	@Override
    protected void onDestroy() {
    	super.onDestroy();
    }
}